﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SYcwpb.Helpers;
using SYcwpb.Models;
using System.Collections.ObjectModel;
using System.Data;
namespace SYcwpb.Access
{
    class DepartmentAccess
    {
        /// <summary>
        /// 获取可以登录的职位，这里与数据库Position表的的Post_type 对应，是否可登录由post_canlogin字段决定
        /// </summary>
        private string SQL_GETALLDEPARTMENT = "select ID,post_type from Position where post_canlogin = 1";

        SqlDataLayer sqlHelper = new SqlDataLayer();

        public ObservableCollection<Department> GetDepartmentList()
        {
            ObservableCollection<Department> _departmentList = new ObservableCollection<Department>();

            DataTable departTB = new DataTable();

            departTB = sqlHelper.SelectDt(SQL_GETALLDEPARTMENT);
            try
            {
                if (departTB.Rows.Count > 0)
                {
                    for (int i = 0; i < departTB.Rows.Count; i++)
                    {
                        Department depart = Department.CreateDepartment();
                        depart.ID = departTB.Rows[i]["ID"].ToString();
                        depart.Name = departTB.Rows[i]["post_type"].ToString();

                        _departmentList.Add(depart);
                    }
                }
            }
            catch (System.Exception ex)
            {
                string err = ex.Message.ToString();

                MessageBoxHelper.ShowError("获取部门信息时出现异常：" + err);
            }
            return _departmentList;
        }
    }
}
